//package com.project.genesis.gateway.filter;
//
//import com.alibaba.fastjson.JSONObject;
//import com.project.genesis.common.constant.Constants;
//import com.project.genesis.common.result.Result;
//import com.project.genesis.common.result.ResultCodeEnum;
//import com.project.genesis.common.util.HttpUtils;
//import org.apache.commons.collections.CollectionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.util.AntPathMatcher;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import java.nio.charset.StandardCharsets;
//import java.util.List;
//
//@Component
//public class AuthGlobalFilter implements GlobalFilter, Ordered {
//
//    private AntPathMatcher antPathMatcher = new AntPathMatcher();
//
//    @Autowired
//    private StringRedisTemplate stringRedisTemplate;
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        ServerHttpRequest request = exchange.getRequest();
//        String path = request.getURI().getPath();
//        // 管理员登录和小程序 登录页面 不过滤
//        if (antPathMatcher.match("/admin/login/**", path)
//                || antPathMatcher.match("/user/appUser/AppLogin", path)
//                || antPathMatcher.match("/user/appUser/AppLoginOut", path)){
//            return chain.filter(exchange);
//        }
////        HttpHeaders headers = request.getHeaders();
////        String headerToken = headers.getFirst("token");
////        String userId = headers.getFirst("userId");
////        if (StringUtils.isBlank(headerToken) || StringUtils.isBlank(userId)){
////            return out(exchange.getResponse(), ResultCodeEnum.LOGIN_AUTH);
////        }
////        String adminTokenKey = "genenis:admin:token:";
////        String userTokenKey = "genenis:user:token:";
////        String adminToken = stringRedisTemplate.opsForValue().get(adminTokenKey+userId).replace("\"","");
////        String userToken = stringRedisTemplate.opsForValue().get(userTokenKey+userId).replace("\"","");
////        if (!headerToken.equals(adminToken) && !headerToken.equals(userToken)){
////            return out(exchange.getResponse(), ResultCodeEnum.TOKEN_ERROR);
////        }
//        return chain.filter(exchange);
//    }
//
//    /**
//     * api接口鉴权失败返回数据
//     * @param response
//     * @return
//     */
//    private Mono<Void> out(ServerHttpResponse response, ResultCodeEnum resultCodeEnum) {
//        Result result = Result.build(null, resultCodeEnum);
//        byte[] bits = JSONObject.toJSONString(result).getBytes(StandardCharsets.UTF_8);
//        DataBuffer buffer = response.bufferFactory().wrap(bits);
//        //指定编码，否则在浏览器中会中文乱码
//        response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
//        return response.writeWith(Mono.just(buffer));
//    }
//
//    @Override
//    public int getOrder() {
//        return 0;
//    }
//
//}
